package apps.softtech.fastmarket.adapter;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.internal.mc;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import apps.softtech.CustomView.SlideHolder;
import apps.softtech.Utility.ApplicationConstants;
import apps.softtech.Utility.ConnectionDetector;
import apps.softtech.Utility.HttpRequest;
import apps.softtech.Utility.RequestDataFromServer;
import apps.softtech.fastmarket.ActivityDashboard;
import apps.softtech.fastmarket.R;
import apps.softtech.fastmarket.model.Category;
import apps.softtech.fastmarket.model.ChatLog;
import apps.softtech.fastmarket.model.DatabaseHandler;

public class AdapterCategoryList extends BaseAdapter{
	DatabaseHandler dbh;
	ArrayList<Category> mCategoriesList = new ArrayList<Category>();
	ArrayList<Category> mCategoriesListChild = new ArrayList<Category>();
	LayoutInflater inf;
	Context mContext;
	ViewFlipper vf ;
	
	ListView lv;
	 TabHost mTabHost;
	 ViewPager mViewPager;
	 SlideHolder mSlideHolder;
	AdpaterCategoryListChild adapter;
	ConnectionDetector cd;
	public AdapterCategoryList(ArrayList<Category> mCategoriesList,Context mContext,ViewFlipper vf,
			ListView lv, TabHost mTabHost,ViewPager mViewPager, SlideHolder mSlideHolder){
		this.mCategoriesList=mCategoriesList;
		this.mContext = mContext;
		inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		dbh = new DatabaseHandler(mContext);
		this.vf=vf;
		this.lv = lv;
		this.mTabHost = mTabHost;
		this.mViewPager = mViewPager;
		this.mSlideHolder = mSlideHolder;
		cd = new ConnectionDetector(mContext);
	}
	@Override
	public int getCount() {
		if(mCategoriesList.size()==0){
			return 1;
		}
		return mCategoriesList.size();
	}
	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View vy, ViewGroup arg2) {
		ViewHolder vh =null;
		  if(mCategoriesList.size()==0||mCategoriesList==null){
			  vy = inf.inflate(R.layout.search_no_result, null);
			  TextView tv = (TextView)vy.findViewById(R.id.tvNoResult);
			  tv.setText(mContext.getResources().getString(R.string.no_category));
			  return vy;
		  }
		if(vy==null){
			vh = new ViewHolder();
			vy = inf.inflate(R.layout.item_category_parent, null);
			vh.tvTextContent = (TextView) vy.findViewById(R.id.tvItemSlideHolder);
			vh.ibNextPage = (Button) vy.findViewById(R.id.ibNextPage);
			vy.setTag(vh);
		}else{
			vh = (ViewHolder) vy.getTag();
		}
		vh.tvTextContent.setText(""+mCategoriesList.get(position).getCategoryName());
		final int id = position;
		vh.ibNextPage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
			int idCatetory = mCategoriesList.get(id).getCategoryId();
			if(!cd.isConnectingToInternet()){
			mCategoriesListChild = (ArrayList<Category>) dbh.getCategory(null, Category.PARENT_CATEGORY_ID
					+ " =?", new String[]{""+idCatetory}, null, null);
			if(mCategoriesListChild.size()!=0){
			setupCategoryChild();
			}
			}
			else{
				new asynctaskGetCategoryChild().execute(idCatetory);
				
			}
			
			
			vf.setInAnimation(mContext, R.anim.in_from_right);
			vf.showNext();
			
	
			}
		});
		return vy;
	}
	/**
	 * setup category child
	 */
	private void setupCategoryChild() {
		adapter = new AdpaterCategoryListChild(mCategoriesListChild, mContext,mTabHost,mViewPager,mSlideHolder);
		lv.setAdapter(adapter);
	}
	 
	 class ViewHolder{
		TextView tvTextContent;
		Button ibNextPage;
		RelativeLayout layoutCategory;
		
	}
	
	class asynctaskGetCategoryChild  extends AsyncTask<Integer, Void, Void>{
		ProgressDialog pd;
		@Override
		protected void onPreExecute() {
			pd = new ProgressDialog(mContext);
			pd.setMessage("Loading....");
			pd.show();
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Integer... arg0) {
			try {
				JSONArray jsonData = new JSONArray();
				JSONObject jsonCategoryParentList = new JSONObject(new RequestDataFromServer().requestCategoryChildList(arg0[0]));
				int success = jsonCategoryParentList.getInt("success");
				if(success==1){
					jsonData = jsonCategoryParentList.getJSONArray("data");
					for(int i=0;i<jsonData.length();i++){
						JSONObject jsonList = new JSONObject(jsonData.getJSONObject(i).toString());
						dbh.insertCategory(new Category(jsonList.getInt("st_CategoryId"),
								jsonList.getString("st_CategoryName"), jsonList.getString("st_Description")
								, jsonList.getInt("st_ParentCategoryId"), jsonList.getInt("st_CategoryLevel")));
					mCategoriesListChild.add(new Category(jsonList.getInt("st_CategoryId"),
							jsonList.getString("st_CategoryName"), jsonList.getString("st_Description")
							, jsonList.getInt("st_ParentCategoryId"), jsonList.getInt("st_CategoryLevel")));
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			setupCategoryChild();
			pd.dismiss();
			
			super.onPostExecute(result);
		}
		
	}
}
